<?
//if($_SESSION['sid']==55) {exit("<script>window.location='dlogin.html'</script>");}
ob_start();
include("common/header.html");
include("common/left.html");
include("common/page.php");
include("class/SimpleExcel.class.php");
$out_html=ob_get_contents();
ob_end_clean();

$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
if($_GET['dtype']==15) //分销联盟分润
    include_once 'distributor_union_lirun.php';

$dtypeT=array(0=>"购买产品",1=>"撤改订单",3=>"账户资金变化",4=>"供应商授信余额",5=>"利润",6=>"提现冻结",7=>"电子凭证费",8=>"短信息费",9=>"银行交易手续费",10=>"凭证费",11=>"供应商信用额度变化 ",12=>"取消提现",13=>"拒绝提现",14=>"退款手续费",15=>'押金');
$ptypeT=array(-1=>"支付宝",0=>"账号资金",1=>"支付宝",2=>"供应商可用资金",3=>"供应授信额度设置",4=>'财付通',5=>'银联',6=>'环迅');
$r=array();
$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:10;
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$offset=($currentPage-1)*$pageSize;
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export")
{
    $currentPage = 1;
    $pageSize = 9999999;
}
$where1=$where2=$where3="where fid=".$_SESSION['sid'];
$where1.=" and dmoney>0 ";
if($beginDate=mysql_real_escape_string($_REQUEST['beginDate'])){
    $where1.=" and rectime>='$beginDate 00:00:00'";
    $where2.=" and rectime>='$beginDate 00:00:00'";
    $where3.=" and dtime>='$beginDate 00:00:00'";
}
if($endDate=mysql_real_escape_string($_REQUEST['endDate'])){
    $where1.=" and rectime<='$endDate 23:59:59'";
    $where2.=" and rectime<='$endDate 23:59:59'";
    $where3.=" and dtime<='$endDate 23:59:59'";
}
if($_REQUEST['daction']==="0"||$_REQUEST['daction']==="1"){
    $daction=$_REQUEST['daction'];
    $where1.=" and daction=$daction";
    if($daction==1){
        $where2="where -1=1";
        $where3="where -1=1";
    }
}
if($_REQUEST['dtype']||$_REQUEST['dtype']==="0"){
    $where1.=" and dtype=".$_REQUEST['dtype'];
    $where2="where -1=1";
    $where3="where -1=1";
}
if($_REQUEST['account']=="pft" || !$_REQUEST['account']){
    $where1.=" and ptype in (0,1,4,5,6)";
    $where2="where -1=1";
    $where3="where -1=1";
}
if($_REQUEST['account']=="credit"){
    $where1.=" and ptype=2";
    $where2="where -1=1";
    $where3="where -1=1";
}
if($_REQUEST['account']=="creditSet"){
    $where1.=" and ptype=3";
    $where2="where -1=1";
    $where3="where -1=1";
}
if($_REQUEST['account']=="alipay"){
    $where1="where -1=1";
//    $where2.=" and fid=".$_SESSION['sid'];
    $where3="where -1=1";
    unset($_REQUEST['dtype']);
}
if($_REQUEST['account']=="tenpay"){
    $where1.=" and ptype in (1,4,5,6)";
    $where2="where -1=1";
    $where3="where -1=1";
}
if($_REQUEST['account']=="unipay"){
    $where1.=" and ptype=5";
    $where2="where -1=1";
    $where3="where -1=1";
}
if($_REQUEST['account']=="ips_pay"){
    $where1.=" and ptype=6";
    $where2="where -1=1";
    $where3="where -1=1";
}

if($_GET['orderid']){
    $where1 .= " and orderid='{$_GET['orderid']}'";
    $where2 .= " and orderid='{$_GET['orderid']}'";
    $where3 .= " and out_trade_no='{$_GET['orderid']}'";
}
$dnames=$orders=array();
$str="select id,dname from pft_member where dtype=0";
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export"){
    $str .=" or dtype=6";
}
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
    $dnames[$row['id']]=$row['dname'];
}

if(isset($_REQUEST['account']) && $_REQUEST['account']=='ipsPay') {
    //TODO:IPS支付记录
    $where3 .= " AND status=1";
    $sql_ips_rec = "SELECT status,total_fee,out_trade_no as orderid,dtime as rectime ".
        "FROM pft_ips_rec {$where3}";
    $GLOBALS['le']->query($sql_ips_rec);
    while($row=$GLOBALS['le']->fetch_assoc()) {
        $row['pay']="<em class='red'>-".($row['total_fee']/100)."</em>";
        $row['dtypeT']="购买产品";
        $row['ptype']=4;
        $r[]=$row;
    }
    $sql_count = "SELECT count(*) as cnt FROM pft_ips_rec {$where3}";
    $GLOBALS['le']->query($sql_count);
    $GLOBALS['le']->fetch_assoc();
    $count = $GLOBALS['le']->f('cnt');
}
else {
    if($_REQUEST['account']=='alipay'){
        $str="select id,fid,money as dmoney,'' as lmoney,'' as aid,orderid,daction,rectime,dtype,-1 as ptype,ali_account from pft_member_alipay $where2 order by rectime desc,orderid desc limit $offset,$pageSize";
    }else{
        $str="select opid,id,fid,dmoney,lmoney,aid,orderid,daction,rectime,dtype,ptype,''".
            " as ali_account,memo from pft_member_journal $where1 order by id desc limit $offset,$pageSize ";
    }
    if($_GET['zmf']){
        echo $str;
    }

    $GLOBALS['le']->query($str);
    while($row=$GLOBALS['le']->fetch_assoc()){
        if($row['daction']==0){
            $row['income']="<em class='green'>+".($row['dmoney']/100).($row['aid']?"</br>(".$dnames[$row['aid']]."授信)</em>":"");
        }elseif($row['daction']==2){
            $row['pay']="<em style='color:#2288EE;'>-".($row['dmoney']/100)."</em>";
        }else{
            $row['pay']="<em class='red'>-".($row['dmoney']/100)."</em>";
        }
        if($row['dtype']==-1){
            $row['dtypeT']="支付宝账号".$row['ali_account'];
            $row['alipay']="账户+".($row['dmoney']/100);
        } elseif ($row['dtype']==-2) {
            $row['dtypeT']="支付宝账号".$row['ali_account'];
            $row['alipay']="账户-".($row['dmoney']/100);
        }
        if($row['dtype']==6&&$row['daction']==0)
        {
            if($row['opid']==$row['fid'])
                $row['dtype']=12;
            if($row['opid']!=$row['fid'])
                $row['dtype']=13;
        }
        $r[]= $row;

    }
}
/*
 * 处理账户余额显示错误的问题 原因是数据库同时对一条数据进行处理 2015-04-17 zax
 * 以最后一个行的余额为基准 array_pop 对其他行的余额进行加减运算来得到正确的余额 不使用从数据库里查出来的值
 * 先倒序一次 方便foreach 再倒回来
*/
goto zzzz;
if(!$_GET['dtype'] && $_GET['account']=="pft"  && count($r)){
    /*
    2015-05-10 zax
    最残酷的情况 只查一条
    传三条记录进去比较
    中间的一条如果和头尾的两条记录的时间和初金额都不一样 则被认定为是正确的数据
    注:初金额指lmoeny与dmoney计算后的结果
    */
    /* 早先版本 一下子把最后三条都传进去 但是没考虑到不足三条的情况
    $last_1 = array_pop($r);//早
    $last_2 = array_pop($r);//中
    $last_3 = array_pop($r);//晚
    $last = array($last_3,$last_2,$last_1);
    */
    $last_1 = array_pop($r);//中
    $last = array('',$last_1,'');
    function get_last_one($last){

        $str="select id,lmoney,rectime,daction,dmoney,dtype,ptype,orderid,memo
              from pft_member_journal
              where fid={$_SESSION['sid']} and dmoney>0 and
              ptype in (0,1,4,5) and rectime<='{$last[1]['rectime']}' and id<{$last[1]['id']}
              order by id desc limit 1";
        $GLOBALS['le']->query($str);
        $last[2]=$GLOBALS['le']->fetch_assoc();//早
        if($last[2]['daction'] == '0') $last[2]['income'] = '+'.$last[2]['dmoney']/100;
        else $last[2]['pay'] = '-'.$last[2]['dmoney']/100;

        if($last[0]!=''){//只有第一层递归会用到这个判断 之后都会存在 不需要再去取了
            $str="select id,lmoney,rectime,daction,dmoney,dtype,ptype,orderid,memo
                  from pft_member_journal
                  where fid={$_SESSION['sid']} and dmoney>0 and
                  ptype in (0,1,4) and rectime>='{$last[1]['rectime']}' and id>{$last[1]['id']}
                  limit 1";
            $GLOBALS['le']->query($str);
            $last[0]=$GLOBALS['le']->fetch_assoc();//晚
            if($last[0]['daction'] == '0') $last[0]['income'] = '+'.$last[0]['dmoney']/100;
            else $last[0]['pay'] = '-'.$last[0]['dmoney']/100;
        }
        foreach ($last as $k => $v) {
            $last[$k]['zmoney'] = $last[$k]['daction'] == 1 ?
                $last[$k]['lmoney'] + $last[$k]['dmoney']: $last[$k]['lmoney'] - $last[$k]['dmoney'];
        }
        if(
            ($last[0]['rectime']==$last[1]['rectime']&& $last[0]['zmoney']==$last[1]['zmoney'] )
            ||
            ($last[2]['rectime']==$last[1]['rectime']&&$last[2]['zmoney']==$last[1]['zmoney'] )
        ){
            //递归查找到最后面一条正确金额的记录作为基准

            $new_last = array($last[1],$last[2],'');
            $new_last = get_last_one($new_last);
            $last[1] = $new_last[0];
        }
        $last[2]['lmoney'] = $last[1]['daction'] == 1 ?
            $last[1]['lmoney'] + $last[1]['dmoney']: $last[1]['lmoney'] - $last[1]['dmoney'];

        $last[0]['lmoney'] = $last[0]['daction'] == 1 ?
            $last[1]['lmoney'] - $last[0]['dmoney']: $last[1]['lmoney'] + $last[0]['dmoney'];
        return $last;
    }
    $last = get_last_one($last);
    $last_1 = $last[1];
    $last_money = $last[1]['lmoney'];
    if(count($r)){//不知道空数组会不会报错 好累 不想测试了 蛮写个判断吧
        krsort($r);
        foreach($r as $k => $v){
            $last_money = $r[$k]['lmoney'] = $v['daction'] == 1 ?
                $last_money - $v['dmoney']: $last_money + $v['dmoney'];
        }
        ksort($r);
    }
    array_push($r,$last_1);
}
zzzz:
if($_REQUEST['account']=='alipay'){
    foreach($r as $k => $row){
        $sql = "select sourceT from pft_alipay_rec where out_trade_no={$row['orderid']} limit 1";
        $GLOBALS['le']->query($sql);
        $GLOBALS['le']->fetch_assoc();
        $row['sourceT'] = $GLOBALS['le']->f('sourceT');
        switch ($row['sourceT']) {
            case '0':
                $r[$k]['ptype']=1;
                break;
            case '1':
                $r[$k]['ptype']=4;
                break;
            case '2':
                $r[$k]['ptype']=5;
                break;
            default:
                $r[$k]['ptype']=1;
        }
    }
}
/* 给百合国旅查账
if($_SESSION['sid']==40142 && $_REQUEST['act']=="Export"){
    foreach($r as $lid=>$row){
        $res[] = $row['orderid'];
    }
    echo '<pre>';
    var_export($res);
    echo '</pre>';
    exit;
}
*/
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export"){

    $filename=date('YmdHis').'交易记录';
    $Excel[0]['rectime']='交易时间';
    $Excel[0]['orderid']='订单号';
    $Excel[0]['dtype']='交易内容';
    $Excel[0]['ptype']='交易方式';
    $Excel[0]['income']='收入';
    $Excel[0]['pay']='支出';
    $Excel[0]['alipay']='银行账户';
    $Excel[0]['lmoney']='账户余额';
    $Excel[0]['lmoney2']='授信余额';
    $Excel[0]['aid']='授信供应商';
    $Excel[0]['opid']='操作人';
    $Excel[0]['ltitle']='产品名称';
    $i=1;
    foreach($r as $lid=>$row){
        //print_r($row);
        $n++;
        $Excel[$n]['rectime']=$row['rectime'];
        $Excel[$n]['orderid']=$row['orderid'];
        $Excel[$n]['dtype']=$dtypeT[$row['dtype']] ?$dtypeT[$row['dtype']]:$row['dtypeT'];
        $Excel[$n]['ptype']=$ptypeT[$row['ptype']];
        $Excel[$n]['income'] = strip_tags($row['income']);
//		$Excel[$n]['pay']=strip_tags($row['pay'])?strip_tags($row['pay']):strip_tags($row['income']);
        $Excel[$n]['pay'] = strip_tags($row['pay']);
        //去掉收入里的授信供应商名字
//        $Excel[$n]['income'] = substr(strip_tags($row['income']),0,strpos(strip_tags($row['income']),'('));
        $Excel[$n]['alipay']=$row['alipay'];
        $Excel[$n]['lmoney']=($row['ptype']==0||$row['ptype']==1||$row['ptype']==4)?$row['lmoney']/100:"";
        $Excel[$n]['lmoney2']=($row['ptype']==2||$row['ptype']==3)?($row['lmoney']/100): "";
        $Excel[$n]['aid']= ($row['ptype']==2||$row['ptype']==3) ? $dnames[$row['aid']] : "";
        $Excel[$n]['opid'] = $dnames[$row['opid']];
        $ltitle = '';
        if($row['orderid']>0){
            $x = simplexml_load_string($s->Order_Globle_Search('','','','','','','','','','','','',$row['orderid'],'','','',''));
            $ltitle = (string)$x->Rec->UUltitle;
        }
        $Excel[$n]['ltitle']=$ltitle;
    }
    // print_r($dtypeT);


    $xls = new SimpleExcel('UTF-8', true,'orderList');
    $xls->addArray($Excel);
    $xls->generateXML($filename);
    exit;
}

echo $out_html;


if($_REQUEST['account']=='alipay'){
    $str="select count(*) as total from pft_member_alipay $where2";
    $GLOBALS['le']->query($str);
    $GLOBALS['le']->fetch_assoc();
    $count2=$GLOBALS['le']->f("total");
    $count1 = 0;
}else{
    $str="select count(*) as total from pft_member_journal $where1";
    $GLOBALS['le']->query($str);
    $GLOBALS['le']->fetch_assoc();
    $count1=$GLOBALS['le']->f("total");
    $count2=0;
}
$count=$count1+$count2;
if(!isset($_REQUEST['account'])) $_REQUEST['account'] = $_GET['account'] = 'pft';


include("tpl/trecord.tpl");
include("common/footer.html");
?>



}